/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.game.entity;

import javax.microedition.lcdui.Graphics;
import towerz.system.Utilities;

/**
 *
 * @author LegACy
 */
public class Bullet {
	protected Bullet() {
		//Initialize
		m_X			= 0;
		m_Y			= 0;
		m_Timer		= 0;
		m_Data		= null;
		m_Target	= null;
		m_Active	= true;
	}

	public Bullet(BulletData data, Enemy target, int x, int y, int bonusDamage) {
		//Default
		this();

		//Save data
		m_X			= x;
		m_Y			= y;
		m_Data		= data;
		m_Target	= target;

		//Calculate bullet direction
		m_BulletDir = BulletData.BULLET_UP; //default
		int dY = (m_Target.getY() - m_Y);
		int dX = (m_Target.getX() - m_X);
		double degree = 0;
		if(dX != 0){
			degree = Math.toDegrees(Utilities.atan((float)dY/dX));
			if(dY < 0){ //Bawah, kiri ke kanan, target di atas
				//if(degree < 0) degree *= -1;
				if(dX > 0) { //Target di kanan
					if (degree > 67.5)		m_BulletDir = BulletData.BULLET_UP;
					else if (degree > 22.5)	m_BulletDir = BulletData.BULLET_UPRIGHT;
					else m_BulletDir = BulletData.BULLET_RIGHT;
				} else { //dX < 0; Target di kiri
					if (degree > 67.5)		m_BulletDir = BulletData.BULLET_UP;
					else if (degree > 22.5)	m_BulletDir = BulletData.BULLET_UPLEFT;
					else m_BulletDir = BulletData.BULLET_LEFT;
				}
			} else { //degree > 0; atas, kanan ke kiri, target di bawah
				if(degree > 0) degree *= -1;
				if(dX > 0) { //target di kanan
					if (degree > -22.5)		m_BulletDir = BulletData.BULLET_RIGHT;
					else if (degree > -67.5)	m_BulletDir = BulletData.BULLET_DOWNRIGHT;
					else m_BulletDir = BulletData.BULLET_DOWN;
				} else { //dX < 0; target di kiri
					if (degree > -22.5)		m_BulletDir = BulletData.BULLET_LEFT;
					else if (degree > -67.5)	m_BulletDir = BulletData.BULLET_DOWNLEFT;
					else m_BulletDir = BulletData.BULLET_DOWN;
				}
			}
		} else {
			if(dY < 0)
				m_BulletDir = BulletData.BULLET_UP;
			else
				m_BulletDir = BulletData.BULLET_DOWN;
		}

		m_BonusDamage = bonusDamage;
	}

	public Enemy getTarget()	{	return m_Target;	}
	public boolean isActive()	{	return m_Active;	}
	public BulletData getData() {	return m_Data;		}
	public int getDamage(){
		return m_Data.getDamage() + m_BonusDamage;
	}

	public void damage(Enemy enemy) {
		//Does buff exist?
		Buff Effect = null;
		if (m_Data.getBuffData() != null) Effect = new Buff(m_Data.getBuffData());

		//Damage enemy
		enemy.damage(getDamage(), Effect);
	}

	public void update(long time) {
		//Skip if no enemy
		if (m_Target == null || m_Target.getHealth() <= 0) m_Active = false;

		//If active
		if (m_Active) {
			//Get difference
			float OffsetFactor = (float)time / (float)(m_Data.getDuration() - m_Timer);

			//Increase timer
			m_Timer += time;
			if (m_Timer >= m_Data.getDuration()) m_Active = false;
			else {
				//Move
				m_X -= OffsetFactor * (m_X - (m_Target.getX() + (Utilities.TILE_WIDTH  / 2)));
				m_Y -= OffsetFactor * (m_Y - (m_Target.getY() + (Utilities.TILE_HEIGHT / 2)));
			}
		}
	}

	public void draw(Graphics g, int startX, int startY, int endX, int endY, int offsetX, int offsetY) {
		//Get offset
		int OffsetX = (startX * Utilities.TILE_WIDTH)  - offsetX;
		int OffsetY = (startY * Utilities.TILE_HEIGHT) - offsetY;

		//In drawing area?

		
		//Draw
		g.drawImage(m_Data.getImage(m_BulletDir), m_X - OffsetX, m_Y - OffsetY, Graphics.HCENTER | Graphics.VCENTER);
	}

	protected int			m_X;
	protected int			m_Y;
	protected long			m_Timer;
	protected BulletData	m_Data;
	protected boolean		m_Active;
	protected Enemy			m_Target;

	protected int			m_BulletDir;
	protected int			m_BonusDamage;
}
